package com.sparrow.common.ai.behaviortree.task.impl;

import com.sparrow.common.ai.behaviortree.def.BTNodeStatus;
import com.sparrow.common.ai.behaviortree.BehaviorContext;
import com.sparrow.common.ai.behaviortree.node.impl.ConditionNode;
import com.sparrow.common.ai.behaviortree.task.BehaviorTask;

/**
 * 条件节点任务 用于监测条件
 */
public class ConditionTask extends BehaviorTask {

    private final ConditionNode condition;
    private boolean value;

    public ConditionTask(ConditionNode node, boolean value) {
        this.condition = node;
        this.value = value;
    }

    public boolean isValue() {
        return value;
    }

    @Override
    public BTNodeStatus tick(BehaviorContext context, long now, long delta) {
        if (value) {
            return condition.check(context) ? BTNodeStatus.SUCCESS : BTNodeStatus.FAILURE;
        } else {
            return condition.check(context) ? BTNodeStatus.FAILURE : BTNodeStatus.SUCCESS;
        }
    }

    public boolean check(BehaviorContext context) {
        return condition.check(context);
    }

    @Override
    public String toString() {
        return "ConditionTask{" +
                "condition=" + condition.getClass().getSimpleName() +
                ", value=" + value +
                '}';
    }
}
